package com.polaris.live.ui.backpack

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.ui.backpack.fragment.ShoppingMallFragment
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.databinding.ActivityBackpackBinding
import com.polaris.live.interface_bag.BackPackSelectCallback
import com.polaris.live.interface_bag.ShoppingCallBack
import com.polaris.live.resp.back_resp.UserBackpackDecorationResp
import com.polaris.live.translation.R
import com.polaris.live.ui.backpack.fragment.BackpackMainFragment
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.getLongOrString
import com.polaris.live.widget.tab.OnTabSelectListener
import com.polaris.live.widget.tab.TabBean

/**
 * BackpackActivity 背包页
 *
 * @author Created by 半仙 on 2024/1/31/031 19:13
 */
class BackpackActivity : BaseVbActivity<BaseViewModel, ActivityBackpackBinding>(),
    BackPackSelectCallback,
    ShoppingCallBack {

    private val mFragments = ArrayList<Fragment>()
    private val mTitles = ArrayList<TabBean>()
    private var tabAdapter: ViewPagerAdapter? = null

    private val decorationType by lazy { intent.getLongOrString("decorationType", -1) }

    private val mIndex by lazy { intent.getIntOrString("index", 0) }

    private val mObjectType by lazy { intent.getIntOrString("objectType", 0) }
    private val mBackPackFragment by lazy {
        BackpackMainFragment().apply {
            arguments = bundleOf(
                "decorationType" to decorationType,
                "objectType" to mObjectType
            )
            setOnBackPackCallback(this@BackpackActivity)
        }
    }

    private val mShoppingMallFragment by lazy {
        ShoppingMallFragment().apply {
            setOnShoppingCallBack(this@BackpackActivity)
        }
    }

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.titleCl)
        attachImmersionBar { transparentStatusBar() }
    }

    override fun initView(savedInstanceState: Bundle?) {
        initTitle()
        mBinding.closeImg.setOnSingleClickListener {
            finish()
        }
        if (resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
            mBinding.closeImg.scaleX = -1f
        }

    }

    private fun initTitle() {
        mTitles.add(TabBean(StringUtils.getString(R.string.backpack)))
        mFragments.add(mBackPackFragment)
        mTitles.add(TabBean(StringUtils.getString(R.string.shopping_mall)))
        mFragments.add(mShoppingMallFragment)

        mBinding.tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                mBinding.pager.currentItem = position
                mBinding.tab.onPageSelected(position)
            }

            override fun onTabReselect(position: Int) {

            }
        })

        mBinding.pager.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mFragments.forEachIndexed { index, fragment ->
                    val show = index != position
                    fragment.onHiddenChanged(show)
                }

            }
        })
        tabAdapter = ViewPagerAdapter(mFragments, this)
        mBinding.pager.adapter = tabAdapter
        mBinding.pager.offscreenPageLimit = 1
        mBinding.tab.setViewPage(mBinding.pager)
        mBinding.tab.setTabs(mTitles)

        mBinding.tab.onPageSelected(mIndex)
        mBinding.pager.currentItem = mIndex
    }

    override fun onBackPackSelectCallback(item: UserBackpackDecorationResp?) {
    }

    override fun goShopping(type: Long) {
        mBinding.tab.onPageSelected(1)
        mBinding.pager.currentItem = 1
        mShoppingMallFragment.goIndex(type)
    }

    override fun buySuccess(type: Long) {
        mBackPackFragment.updateData(type)
    }

    override fun toLook(type: Long) {
        mBinding.tab.onPageSelected(0)
        mBinding.pager.currentItem = 0
        mBackPackFragment.goIndex(type)
    }

}